<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>百度贴吧操作控制</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            margin: 0;
            padding: 0;
            background-color: #f8f9fa;
        }

        header {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 60px;
            background-color: #e9ecef;
        }

        header h1 {
            font-size: 1.5rem;
            color: #333;
        }

        main {
            display: flex;
            flex: 1;
            background-color: #e9ecef;
        }

      .left-section {
            width: 50%;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            overflow-y: auto;
        }

      .right-section {
            width: 50%;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            overflow-y: auto;
        }

      .right-section p {
            margin: 0;
            padding: 8px;
            background-color: #f8f9fa;
            border-radius: 3px;
            box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
            color: #333;
            line-height: 1.4;
        }

        footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 60px;
            background-color: #e9ecef;
            padding: 0 20px;
        }

        button {
            width: 48%;
            padding: 12px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 3px;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* 新增样式用于调整label和input的布局 */
        label,
        input,
        textarea {
            font-size: 120%;
        }

        label {
            display: inline-block;
            width: 30%;
            text-align: right;
            margin-right: 10px;
        }

        textarea {
            display: inline-block;
            width: 100%;
            height: 20%;
            text-align: right;
            margin-right: 10px;
        }
    </style>
</head>

<body>
    <header>
        <h1>百度贴吧操作控制</h1>
    </header>
    <main>
        <div class="left-section">
            <label for="cookie">Cookie：</label>
            <br/>
            <textarea id="cookie" placeholder="输入Cookie">2</textarea><br>

            <label for="fans.enable">粉丝清理：</label>
            <input type="checkbox" id="fans.enable"><br/>
            <label for="fans.start">开始页：</label>
            <input type="number" id="fans.start" placeholder="输入开始页" value="1"><br/>
            <label for="fans.end">结束页：</label>
            <input type="number" id="fans.end" placeholder="输入结束页" value="2"><br/>
<hr/>
            <label for="concern.enable">关注清理：</label>
            <input type="checkbox" id="concern.enable"><br/>
            <label for="concern.start">开始页：</label>
            <input type="number" id="concern.start" placeholder="输入开始页" value="1"><br/>
            <label for="concern.end">结束页：</label>
            <input type="number" id="concern.end" placeholder="输入结束页" value="2"><br/>
            <hr/>
            <label for="tieba.enable">贴吧关注清理：</label>
            <input type="checkbox" id="tieba.enable"><br/>
            <label for="tieba.start">开始页：</label>
            <input type="number" id="tieba.start" placeholder="输入开始页" value="1"><br/>
            <label for="tieba.end">结束页：</label>
            <input type="number" id="tieba.end" placeholder="输入结束页" value="2"><br/>
            <hr/>
            <label for="post.enable">发帖清理：</label>
            <input type="checkbox" id="post.enable"><br/>
            <label for="post.start">开始页：</label>
            <input type="number" id="post.start" placeholder="输入开始页" value="1"><br/>
            <label for="post.end">结束页：</label>
            <input type="number" id="post.end" placeholder="输入结束页" value="2"><br/>
            <hr/>
            <label for="reply.enable">回帖清理：</label>
            <input type="checkbox" id="reply.enable"><br/>
            <label for="reply.start">开始页：</label>
            <input type="number" id="reply.start" placeholder="输入开始页" value="1"><br/>
            <label for="reply.end">结束页：</label>
            <input type="number" id="reply.end" placeholder="输入结束页" value="2"><br/>
            <hr/>
        </div>
        <div class="right-section" id="messageDisplay">
        </div>
    </main>
    <footer>
        <button id="startButton">开始操作</button>
        <button id="startButton">停止操作</button>
    </footer>

    <script>
        let port = window.location.port;
        let hostname = window.location.hostname;
        const ws = new WebSocket('ws://' + hostname + ':' + port);

        const messageDisplay = document.getElementById('messageDisplay');

        ws.onopen = function () {
            appendMessage('WebSocket连接已建立');
        };

        ws.onmessage = function (event) {
            const data = JSON.parse(event.data);
            appendMessage(data.message);
        };

        ws.onerror = function (error) {
            appendMessage('WebSocket连接出错：' + error);
        };

        ws.onclose = function () {
            appendMessage('WebSocket连接已关闭');
        };

        const startButton = document.getElementById('startButton');
        const stopButton = document.getElementById('stopButton');

        startButton.addEventListener('click', function () {
            const cookie = document.getElementById('cookie').value;

            const fansEnable = document.getElementById('fans.enable').checked;
            const fansstart = document.getElementById('fans.start').value;
            const fansend = document.getElementById('fans.end').value;

            const concernEnable = document.getElementById('concern.enable').checked;
            const concernstart = document.getElementById('concern.start').value;
            const concernend = document.getElementById('concern.end').value;

            const tiebaEnable = document.getElementById('tieba.enable').checked;
            const tiebastart = document.getElementById('tieba.start').value;
            const tiebaend = document.getElementById('tieba.end').value;

            const postEnable = document.getElementById('post.enable').checked;
            const poststart = document.getElementById('post.start').value;
            const postend = document.getElementById('post.end').value;

            const replyEnable = document.getElementById('reply.enable').checked;
            const replystart = document.getElementById('reply.start').value;
            const replyend = document.getElementById('reply.end').value;

            if (!cookie) {
                alert('请输入Cookie');
                return;
            }
            if ((fansEnable && (!fansstart ||!fansend)) ||
                (concernEnable && (!concernstart ||!concernend)) ||
                (tiebaEnable && (!tiebastart ||!tiebaend)) ||
                (postEnable && (!poststart ||!postend)) ||
                (replyEnable && (!replystart ||!replyend))) {
                alert('请输入有效的开始页和结束页');
                return;
            }

            const message = {
                type: 'start',
                cookie: cookie,
                fans: {
                    enable: fansEnable,
                    start: parseInt(fansstart),
                    end: parseInt(fansend)
                },
                concern: {
                    enable: concernEnable,
                    start: parseInt(concernstart),
                    end: parseInt(concernend)
                },
                tieba: {
                    enable: tiebaEnable,
                    start: parseInt(tiebastart),
                    end: parseInt(tiebaend)
                },
                post: {
                    enable: postEnable,
                    start: parseInt(poststart),
                    end: parseInt(postend)
                },
                reply: {
                    enable: replyEnable,
                    start: parseInt(replystart),
                    end: parseInt(replyend)
                }
            };

            ws.send(JSON.stringify(message));
        });

        stopButton.addEventListener('click', function () {
            const message = JSON.stringify({ type: 'stop' });
            ws.send(message);
        });

        function appendMessage(message) {
            const p = document.createElement('p');
            p.textContent = message;
            messageDisplay.appendChild(p);
        }
    </script>
</body>

</html>